<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <title>深度监视简写</title>
</head>

<body>
    <div id="root">
        <h2>今天天气很{{info}}</h2>
        <button @click="change">切换天气</button>
    </div>

    <script>
        const vm = new Vue({
            el: "#root",
            data: {
                Hot: true,
            },
            computed: {
                info() {
                    return this.Hot ? "炎热" : "凉爽"
                }
            },
            methods: {
                change() {
                    this.Hot = !this.Hot
                }
            },
            //    监视属性必须存在才能监视  可以监视属性和计算属性
            watch: {
                /*Hot: {
                    // immediate: true,//初始化的时候调用handler
                    // deep:true,//深度监视 默认为false
                    handler(newValue, oldValue) {
                        console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                },*/
                // 不需要immediate deep等配置项的时候可以简写
                Hot(newValue, oldValue) {
                    console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
                },

                info: {
                    immediate: true,//初始化的时候调用handler
                    handler(newValue, oldValue) {
                        console.log("info被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                },
            }
        })
        // 正常写法 vm参数的监视
        /*vm.$watch("Hot", {
            immediate: true,//初始化的时候调用handler
            deep: true,//深度监视 默认为false
            handler(newValue, oldValue) {
                console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
            }
        })*/

        //简写形式
        vm.$watch("Hot",function(newValue,oldValue){
            console.log("Hot被修改了", "更改后是" + newValue)
        }) 

    </script>

</body>

</html>